package com.qianying.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qianying.comment.CommonResult;
import com.qianying.entity.MallInfo;
import com.qianying.service.MallInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商场信息控制器
 * 增强版分页查询支持省ID、市ID、区ID和状态筛选
 */
@RestController
@RequestMapping("/admin/mallInfo")
@Tag(name = "商场信息管理(商场资料)", description = "商场信息的增删改查及高级筛选接口")
public class MallInfoController {

    @Autowired
    private MallInfoService mallInfoService;

    /**
     * 分页查询商场信息列表
     * 支持按省ID、市ID、区ID和状态筛选，参数可选
     * @param current 页码
     * @param size 每页条数
     * @param provinceId 省ID（对应china_area表的id）
     * @param cityId 市ID（对应china_area表的id）
     * @param districtId 区ID（对应china_area表的id）
     * @param status 状态（0-禁用，1-启用）
     * @param mallName 商场名称（模糊查询）
     * @param mallCode 商场编号（精确查询）
     * @return 分页结果
     */
    @GetMapping("/getPage")
    @Operation(
            summary = "分页查询商场信息列表",
            description = "支持按省ID、市ID、区ID和状态筛选，所有参数均为可选，不传递则不筛选",
            parameters = {
                    @Parameter(name = "current", description = "页码，默认1", example = "1"),
                    @Parameter(name = "size", description = "每页条数，默认10", example = "10"),
                    @Parameter(name = "provinceId", description = "省ID", example = "100000"),
                    @Parameter(name = "provinceName", description = "省", example = "100000"),
                    @Parameter(name = "cityId", description = "市ID", example = "7"),
                    @Parameter(name = "cityName", description = "市区", example = "7"),
                    @Parameter(name = "districtId", description = "区ID", example = "16"),
                    @Parameter(name = "districtName", description = "区", example = "16"),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1"),
                    @Parameter(name = "mallName", description = "商场名称（模糊查询）", example = "天河商场"),
                    @Parameter(name = "mallCode", description = "商场编号（精确查询）", example = "201")
            }
    )
    public CommonResult<IPage<MallInfo>> getPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long provinceId,
            @RequestParam(required = false) Long cityId,
            @RequestParam(required = false) Long districtId,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String mallName,
            @RequestParam(required = false) String mallCode) {

        // 构建查询条件对象
        MallInfo query = new MallInfo();
        query.setProvinceId(provinceId != null ? provinceId.toString() : null);
        query.setCityId(cityId != null ? cityId.toString() : null);
        query.setDistrictId(districtId != null ? districtId.toString() : null);
        query.setStatus(status);
        query.setMallName(mallName);
        query.setMallCode(mallCode);

        // 执行分页查询
        Page<MallInfo> page = new Page<>(current, size);
        return mallInfoService.getPage(page, query);
    }

    /**
     * 根据ID查询商场信息详情
     * @param id 主键ID
     * @return 商场信息详情
     */
    @GetMapping("/getById")
    @Operation(
            summary = "查询商场信息详情",
            description = "通过ID获取商场的完整信息",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<MallInfo> getById(@RequestParam Long id) {
        return mallInfoService.getById(id);
    }

    /**
     * 新增商场信息
     * @param mallInfo 商场信息（ID需为空）
     * @return 操作结果
     */
    @PostMapping("/saveMall")
    @Operation(
            summary = "新增商场信息",
            description = "创建新的商场记录，商场名称为必填项，ID必须为空",
            parameters = {
                    @Parameter(name = "mallName", description = "商场名称", required = true, example = "天河商场"),
                    @Parameter(name = "mallCode", description = "商场编号", example = "201"),
                    @Parameter(name = "provinceId", description = "省编Id", example = "7"),
                    @Parameter(name = "cityId", description = "市Id", example = "8"),
                    @Parameter(name = "districtId", description = "区/县Id", example = "16"),
                    @Parameter(name = "mallAddress", description = "商场详细地址", example = "天河路111号"),
                    @Parameter(name = "handlingFee", description = "办证费", example = "25.00"),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1"),
                    @Parameter(name = "description", description = "商场描述", example = "地处体育西中心地带的优质商场")
            }
    )
    public CommonResult<?> saveMall(@RequestBody MallInfo mallInfo) {
        return mallInfoService.saveMall(mallInfo);
    }

    /**
     * 更新商场信息
     * @param mallInfo 商场信息（需包含ID）
     * @return 操作结果
     */
    @PostMapping("/updateMall")
    @Operation(
            summary = "更新商场信息",
            description = "修改已有商场信息，必须包含ID和商场名称",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "mallName", description = "商场名称", required = true, example = "天河商场"),
                    @Parameter(name = "mallCode", description = "商场编号", example = "201"),
                    @Parameter(name = "provinceId", description = "省Id", example = "7"),
                    @Parameter(name = "cityId", description = "市Id", example = "8"),
                    @Parameter(name = "districtId", description = "区Id", example = "16"),
                    @Parameter(name = "mallAddress", description = "商场详细地址", example = "天河路111号"),
                    @Parameter(name = "handlingFee", description = "办证费", example = "25.00"),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", example = "1"),
                    @Parameter(name = "description", description = "商场描述", example = "地处体育西中心地带的优质商场")
            }
    )
    public CommonResult<?> updateMall(@RequestBody MallInfo mallInfo) {
        return mallInfoService.updateMall(mallInfo);
    }

    /**
     * 更新商场启用状态
     * @param id 主键ID
     * @param status 启用状态（0-禁用，1-启用）
     * @return 操作结果
     */
    @PostMapping("/updateStatus")
    @Operation(
            summary = "更新商场状态",
            description = "单独更新商场的启用/禁用状态",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1"),
                    @Parameter(name = "status", description = "状态（0-禁用，1-启用）", required = true, example = "1")
            }
    )
    public CommonResult<?> updateStatus(
            @RequestParam Long id,
            @RequestParam Integer status) {
        return mallInfoService.updateStatus(id, status);
    }

    /**
     * 根据ID删除商场信息
     * @param id 主键ID
     * @return 操作结果
     */
    @PostMapping("/removeById")
    @Operation(
            summary = "删除商场信息",
            description = "通过ID删除指定的商场记录",
            parameters = {
                    @Parameter(name = "id", description = "主键ID", required = true, example = "1")
            }
    )
    public CommonResult<?> removeById(@RequestParam Long id) {
        return mallInfoService.removeById(id);
    }

    /**
     * 查询所有商场信息（不分页）
     * @return 商场信息列表
     */
    @GetMapping("/getAll")
    @Operation(
            summary = "查询所有商场信息",
            description = "返回所有商场信息列表，适用于下拉选择等场景"
    )
    public CommonResult<List<MallInfo>> getAll() {
        return mallInfoService.getAll();
    }
}
